﻿using HardcoreGame.Common.Systems;
using HardcoreGame.Content.Items.Placeable.Banners;
using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.GameContent.Bestiary;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.NPCs.Enemies.Events.GeomagneticStorm
{
    public class StaticSpider : ModNPC
    {
        public override void SetDefaults()
        {
            NPC.lifeMax = 50;
            NPC.damage = 12;
            NPC.defense = 6;
            NPC.knockBackResist = 0f;
            NPC.width = 34;
            NPC.height = 36;
            NPC.aiStyle = -1;
            NPC.npcSlots = 0.5f;
            NPC.HitSound = SoundID.NPCHit1;
            NPC.noGravity = true;
            NPC.buffImmune[20] = true;
            NPC.buffImmune[70] = true;
            NPC.npcSlots = 2f;
            NPC.DeathSound = SoundID.NPCDeath1;
            NPC.value = Item.buyPrice(0, 0, 1, 15);
            AnimationType = 238;
            NPC.rarity = 1;
            SpawnModBiomes = [ModContent.GetInstance<Biomes.GeomagneticStorm>().Type];
            Banner = NPC.type;
            BannerItem = ModContent.ItemType<StaticSpiderBanner>();
        }
        public override void SetBestiary(BestiaryDatabase database, BestiaryEntry bestiaryEntry)
        {
            bestiaryEntry.Info.AddRange([
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Biomes.Underground,
                new FlavorTextBestiaryInfoElement("Mods.HardcoreGame.Bestiary.StaticSpider")
            ]);
        }
        public override float SpawnChance(NPCSpawnInfo spawnInfo)
        {
            if (EventSystem.activeEvents.Contains(3) && spawnInfo.Player.ZoneNormalUnderground)
            {
                return 0.5f;
            }
            return 0f;
        }
        public override void SetStaticDefaults()
        {
            NPCID.Sets.NPCBestiaryDrawModifiers value = new() { Rotation = -MathHelper.PiOver2 };
            NPCID.Sets.NPCBestiaryDrawOffset[Type] = value;
            Main.npcFrameCount[NPC.type] = 4;
        }
        public override void ApplyDifficultyAndPlayerScaling(int numPlayers, float balance, float bossAdjustment)
        {
            NPC.lifeMax = NPC.lifeMax;
            NPC.damage = NPC.damage;
        }
        public override void AI()
        {
            NPC.TargetClosest(true);
            Player player = Main.player[NPC.target];
            if (NPC.target < 0 || NPC.target == 255 || Main.player[NPC.target].dead)
            {
                NPC.TargetClosest(true);
            }
            float num = 2f;
            float num2 = 0.08f;
            Vector2 vector;
            vector = new(NPC.position.X + NPC.width * 0.5f, NPC.position.Y + NPC.height * 0.5f);
            float num3 = Main.player[NPC.target].position.X + Main.player[NPC.target].width / 2;
            float num4 = Main.player[NPC.target].position.Y + Main.player[NPC.target].height / 2;
            float num5 = (int)((double)num3 / 8.0) * 8;
            float num6 = (int)((double)num4 / 8.0) * 8;
            vector.X = (int)(vector.X / 8.0) * 8;
            vector.Y = (int)(vector.Y / 8.0) * 8;
            float num7 = num5 - vector.X;
            float num8 = num6 - vector.Y;
            float num9 = (float)Math.Sqrt((double)num7 * (double)num7 + (double)num8 * (double)num8);
            float num10;
            float num11;
            if ((double)num9 == 0.0)
            {
                num10 = NPC.velocity.X;
                num11 = NPC.velocity.Y;
            }
            else
            {
                float num12 = num / num9;
                num10 = num7 * num12;
                num11 = num8 * num12;
            }
            if (Main.player[NPC.target].dead)
            {
                num10 = (float)(NPC.direction * (double)num / 2.0);
                num11 = (float)(-(float)(double)num / 2.0);
            }
            NPC.spriteDirection = -1;
            if (!Collision.CanHit(NPC.position, NPC.width, NPC.height, Main.player[NPC.target].position, Main.player[NPC.target].width, Main.player[NPC.target].height))
            {
                NPC.ai[0] += 1f;
                if (NPC.ai[0] > 0.0)
                {
                    NPC.velocity.Y = NPC.velocity.Y + 0.023f;
                }
                else
                {
                    NPC.velocity.Y = NPC.velocity.Y - 0.023f;
                }
                if (NPC.ai[0] < -100.0 || NPC.ai[0] > 100.0)
                {
                    NPC.velocity.X = NPC.velocity.X + 0.023f;
                }
                else
                {
                    NPC.velocity.X = NPC.velocity.X - 0.023f;
                }
                if (NPC.ai[0] > 200.0)
                {
                    NPC.ai[0] = -200f;
                }
                NPC.velocity.X = NPC.velocity.X + num10 * 0.007f;
                NPC.velocity.Y = NPC.velocity.Y + num11 * 0.007f;
                NPC.rotation = (float)Math.Atan2(NPC.velocity.Y, NPC.velocity.X);
                if (NPC.velocity.X > 1.5)
                {
                    NPC.velocity.X = NPC.velocity.X * 0.9f;
                }
                if (NPC.velocity.X < -1.5)
                {
                    NPC.velocity.X = NPC.velocity.X * 0.9f;
                }
                if (NPC.velocity.Y > 1.5)
                {
                    NPC.velocity.Y = NPC.velocity.Y * 0.9f;
                }
                if (NPC.velocity.Y < -1.5)
                {
                    NPC.velocity.Y = NPC.velocity.Y * 0.9f;
                }
                if (NPC.velocity.X > 3.0)
                {
                    NPC.velocity.X = 3f;
                }
                if (NPC.velocity.X < -3.0)
                {
                    NPC.velocity.X = -3f;
                }
                if (NPC.velocity.Y > 3.0)
                {
                    NPC.velocity.Y = 3f;
                }
                if (NPC.velocity.Y < -3.0)
                {
                    NPC.velocity.Y = -3f;
                }
            }
            else
            {
                if (NPC.velocity.X < (double)num10)
                {
                    NPC.velocity.X = NPC.velocity.X + num2;
                    if (NPC.velocity.X < 0.0 && (double)num10 > 0.0)
                    {
                        NPC.velocity.X = NPC.velocity.X + num2;
                    }
                }
                else if (NPC.velocity.X > (double)num10)
                {
                    NPC.velocity.X = NPC.velocity.X - num2;
                    if (NPC.velocity.X > 0.0 && (double)num10 < 0.0)
                    {
                        NPC.velocity.X = NPC.velocity.X - num2;
                    }
                }
                if (NPC.velocity.Y < (double)num11)
                {
                    NPC.velocity.Y = NPC.velocity.Y + num2;
                    if (NPC.velocity.Y < 0.0 && (double)num11 > 0.0)
                    {
                        NPC.velocity.Y = NPC.velocity.Y + num2;
                    }
                }
                else if (NPC.velocity.Y > (double)num11)
                {
                    NPC.velocity.Y = NPC.velocity.Y - num2;
                    if (NPC.velocity.Y > 0.0 && (double)num11 < 0.0)
                    {
                        NPC.velocity.Y = NPC.velocity.Y - num2;
                    }
                }
                NPC.rotation = (float)Math.Atan2((double)num11, (double)num10);
            }
            float num13 = 0.5f;
            if (NPC.collideX)
            {
                NPC.netUpdate = true;
                NPC.velocity.X = NPC.oldVelocity.X * -num13;
                if (NPC.direction == -1 && NPC.velocity.X > 0.0 && NPC.velocity.X < 2.0)
                {
                    NPC.velocity.X = 2f;
                }
                if (NPC.direction == 1 && NPC.velocity.X < 0.0 && NPC.velocity.X > -2.0)
                {
                    NPC.velocity.X = -2f;
                }
            }
            if (NPC.collideY)
            {
                NPC.netUpdate = true;
                NPC.velocity.Y = NPC.oldVelocity.Y * -num13;
                if (NPC.velocity.Y > 0.0 && NPC.velocity.Y < 1.5)
                {
                    NPC.velocity.Y = 2f;
                }
                if (NPC.velocity.Y < 0.0 && NPC.velocity.Y > -1.5)
                {
                    NPC.velocity.Y = -2f;
                }
            }
            if ((NPC.velocity.X > 0.0 && NPC.oldVelocity.X < 0.0 || NPC.velocity.X < 0.0 && NPC.oldVelocity.X > 0.0 || NPC.velocity.Y > 0.0 && NPC.oldVelocity.Y < 0.0 || NPC.velocity.Y < 0.0 && NPC.oldVelocity.Y > 0.0) && !NPC.justHit)
            {
                NPC.netUpdate = true;
            }
            if (Main.netMode == 1)
            {
                return;
            }
            if (Main.netMode != 1 && Main.expertMode && NPC.target >= 0 && Collision.CanHit(NPC.Center, 1, 1, Main.player[NPC.target].Center, 1, 1))
            {
                NPC.localAI[0] += 1f;
                if (NPC.justHit)
                {
                    NPC.localAI[0] -= Main.rand.Next(20, 60);
                    if (NPC.localAI[0] < 0.0)
                    {
                        NPC.localAI[0] = 0f;
                    }
                }
                if (NPC.localAI[0] > 225.0)
                {
                    NPC.localAI[0] = 0f;
                }
            }
            int num14 = (int)NPC.Center.X / 16;
            int num15 = (int)NPC.Center.Y / 16;
            bool flag = false;
            for (int i = num14 - 1; i <= num14 + 1; i++)
            {
                for (int j = num15 - 1; j <= num15 + 1; j++)
                {
                    if (Main.tile[i, j] == null)
                    {
                        return;
                    }
                    if (Main.tile[i, j].WallType > 0)
                    {
                        flag = true;
                    }
                }
            }
            if (flag)
            {
                return;
            }
            NPC.Transform(ModContent.NPCType<StaticSpiderGround>());
        }
        public override void HitEffect(NPC.HitInfo hit)
        {
            if (NPC.life <= 0)
            {
                for (int i = 0; i < 20; i++)
                {
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, 226, 2.5f * hit.HitDirection, -2.5f, 0, default, 1f);
                }
                return;
            }
            int num = 0;
            while (num < hit.Damage / (double)NPC.lifeMax * 50.0)
            {
                Dust.NewDust(NPC.position, NPC.width, NPC.height, 226, hit.HitDirection, -1f, 0, default, 0.8f);
                num++;
            }
        }
        public override void OnHitPlayer(Player target, Player.HurtInfo hurtInfo)
        {
            target.AddBuff(BuffID.Electrified, 360);
        }
    }
}
